package pl.edu.pw.perg.compinfodb.manager;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import pl.edu.pw.perg.compinfodb.dao.IHistoryDao;
import pl.edu.pw.perg.compinfodb.entities.HistoryItem;

public class HistoryManager extends GenericManager<HistoryItem> implements
		IHistoryManager {

	private Map<Long, List<HistoryItem>> cachedHistoryMap = new HashMap<Long, List<HistoryItem>>();

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * pl.edu.pw.perg.compinfodb.manager.IHistoryManager#findElementHistory(
	 * java.lang.Long)
	 */
	public List<HistoryItem> findElementHistory(Long elementId) {
		return ((IHistoryDao) getGenericDAO()).findElementHistory(elementId);
	}

	public List<HistoryItem> getElementHistory(Long elementId) {
		if (cachedHistoryMap.containsKey(elementId))
			return cachedHistoryMap.get(elementId);
		List<HistoryItem> freshList = findElementHistory(elementId);
		cachedHistoryMap.put(elementId, freshList);
		return freshList;
	}

	public List<HistoryItem> refreshHistory(Long elementId) {
		List<HistoryItem> freshList = findElementHistory(elementId);
		cachedHistoryMap.get(elementId).addAll(freshList);
		// cachedHistoryMap.put(elementId, freshList);
		return freshList;
	}

}
